/**
 * 请求后台服务接口的处理方法
 * 外部调用此方法，传入接口最终返回数据，通过这里处理
 */
 import { ClearSessionStorage } from "sept-ui/lib/utils/communicate/auxiliary.builtin";
 import $tool from 'sept-ui/lib/utils/arms/tools.rich';
 
 const Dispose = resultData => {
     let _result = {
         status: "",
         data: "",
         message: "",
         scode: ""
     };
     //如果接口全部正确就返回最后数据结果
     if (
         resultData?.status &&
         resultData.status === 200 &&
         resultData.data.sys_header.status === "ok"
     ) {
         _result.status = 'ok';
         _result.data = resultData?.data.appdata;
     } else if (
         resultData?.data &&
         resultData?.data.sys_header?.status === "fail"
     ) {
         _result.status = "fail";
         _result.message = resultData?.data.sys_header.message
             ? resultData?.data.sys_header.message
             : "";
         _result.scode = resultData?.data.sys_header.scode
             ? resultData?.data.sys_header.scode
             : "";
         if (resultData?.data.sys_header.scode === "SYT_TOKEN_OUT_TIME") {
             //token失效
             console.log("token失效");
             ClearSessionStorage();
             location.reload(); // 强制刷新
             return;
         }
     } else {
         //jwt 401返回，跳出登录
         if(JSON.stringify(resultData).includes('"status":401')){
             console.log('jwt token 失效')
             ClearSessionStorage();
             localStorage.clear();
             location.reload(); // 强制刷新
             return;
         }
         _result.message =
             resultData.message && resultData.message != ""
                 ? resultData.message
                 : "未知错误";
     }
     //返回值提示优化
     if (_result.scode == "SYS_UNKNOWN_EXCEPTION") {
         _result.message = "未知异常";
     }
     //清除操作审计参数
     let cd = $tool.GetSessionStorage("CommData");
     if(cd){
         let param = JSON.parse(cd);
         param.pend_srv_name = '';
         param.pendwk_bk_expl = '';
         param.apply_html = '';
         param.org_work_code = '';
         $tool.SetSessionStorage("CommData", JSON.stringify(param));
     }
     return _result;
 };
 
 /**
  * 公共函数调用
  * @param {this} _this 
  * @param {服务名} api 
  * @param {参数} param 
  * @param {回调函数} callback 
  * @param {加载标识} loading
  */
 let requests = 0;
 const Result = (_this, api, param, callback, loading=true) => {
    if(loading){
        ++requests;
        if(requests==0){
            _this.$_F.Loading();
        }
    }
    _this[api](param).then(res => {
        if(loading) --requests;
        if (res.status === "ok") {
            callback(res);
        } else {
            _this.$Message.error(res.message);
        }
        if(loading&&requests==0) _this.$_F.LoadingHide();
    }).catch(err => {
        console.error("请求异常", err);
        if(loading) _this.$_F.LoadingHide();
    });
 }
 
 export { Dispose, Result };
 